/*
	FileName : JT_C_COMM_SM.c
	Description :
		Manage the JT_C host communication state.
	
	Created on 2017.11.18 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/
#include "JT_C_COMM_SM.h"
#include "JT_C_REC_Item.h"

//---------------------------------------------------------------------
JTC_COMM_SM uart1_SM;
extern uint8_t Tax39_Buf[];


//------------------
void JTC_COMM_SM_CopyBoardData(JTC_COMM_SM *sm)
{
	uint16_t k;
	
	k = StsCtrl.readyIndex;
	
	STS_FRAME_setBdValidBit_board(&StsFrame[k],&StsCtrl);
	
	STS_FRAME_copyIn_ctrlA(&StsFrame[k],sm->ZJA_DATA);
	STS_FRAME_copyIn_ctrlB(&StsFrame[k],sm->ZJB_DATA);
	STS_FRAME_copyIn_LJB(&StsFrame[k],sm->LJB_DATA);	
	
	STS_FRAME_setCRC(&StsFrame[k]);
			
	if ((StsFrame[k].CtrlABuf[2] & 0x80) || (StsFrame[k].CtrlBBuf[2] & 0x80))
		StsFrame[k].ex_CoilErr = 1;
	else
		StsFrame[k].ex_CoilErr = 0;
	
	STS_FRAME_setMainUseStatus(&StsFrame[k],&StsCtrl);
	
	StsCtrl.readyIndex = STS_FRAME_indexAdd(StsCtrl.readyIndex);
		
}
//------------------------------------------------

void JTC_COMM_SM_init(JTC_COMM_SM *sm)
{
	sm->currState = JTC_COMM_SM_STATE_IDEL;
	sm->monitorTick = 0;
	sm->smTick = 0;
	sm->sm_time = 0;
	
	sm->ZJA_errCnt = 0;
	sm->ZJB_errCnt = 0;
	sm->LJB_errCnt = 0;
	sm->JLB_Xfer_cnt = 0;
	
	sm->ex_VersionFlag = 0;
	sm->in_VersionFlag = 0;
	sm->JW_Flag = 0;
	
	//----------------------
	sm->uartReceiveFlag = 0;
	sm->uartReceiveDataLen = 0;
	
	sm->uartTransmitFlag = 0;
	sm->uartTransmitDataLen = 0;
	return;
}

void JTC_COMM_SM_stepTick(JTC_COMM_SM *sm)
{
	sm->smTick++;
	return;
}
//-----------------------------------------------------
void JTC_COMM_SM_setMonitortick(JTC_COMM_SM *sm)
{
	sm->monitorTick = sm->smTick;
	return;
}

uint32_t JTC_COMM_SM_tickElapsed(JTC_COMM_SM *sm)
{
	if(sm->smTick < sm->monitorTick)
		return 0xFFFFFFFF;
	else
		return (sm->smTick - sm->monitorTick);
}
/// ------------------------------------------------------------------
/// call by user task circularly
/// ------------------------------------------------------------------
void JTC_COMM_SM_main(JTC_COMM_SM *sm)
{
	uint16_t i;
	uint8_t data[8],*ptr;
	switch(sm->currState){
		
		case JTC_COMM_SM_STATE_IDEL:
			if(StsCtrl.adcCreatIndex != StsCtrl.readyIndex)
				sm->currState = JTC_COMM_SM_STATE_i_ZKA;
			else if(StsCtrl.readyIndex != StsCtrl.handledIndex)
				sm->currState = JTC_COMM_SM_STATE_BROADCAST;
			else if(sm->smTick > 30000 && !sm->JW_Flag)
				sm->currState = JTC_COMM_SM_STATE_i_JW;
			else if(sm->smTick > 32000 && !sm->ex_VersionFlag)
				sm->currState = JTC_COMM_SM_STATE_i_ExV_ZKA;
			else if(sm->smTick > 34000 && !sm->in_VersionFlag)
				sm->currState = JTC_COMM_SM_STATE_i_InV_ZKA;
			break;
			
		//--------------------------------	
		case JTC_COMM_SM_STATE_i_ZKA:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKA,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_INQUIRY_WORK_INFO,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ZKA;
			break;
		case JTC_COMM_SM_STATE_w_ZKA:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				for(i=0;i<sm->uartReceiveDataLen;i++)
					sm->ZJA_DATA[i] = sm->uartReceiveData[i];
				//set board valid
				StsCtrl.BoardValid |= (1 << 0);
				sm->ZJA_errCnt = 0;
				sm->currState = JTC_COMM_SM_STATE_i_ZKB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_100ms)
			{
				//set boart unvalid
				sm->ZJA_errCnt++;
				if(sm->ZJA_errCnt >4)
					StsCtrl.BoardValid &= (~(1 << 0));
				sm->currState = JTC_COMM_SM_STATE_i_ZKB;
			}
			break;
				
		case JTC_COMM_SM_STATE_i_ZKB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKB,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_INQUIRY_WORK_INFO,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ZKB;
			break;
		case JTC_COMM_SM_STATE_w_ZKB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				for(i=0;i<sm->uartReceiveDataLen;i++)
					sm->ZJB_DATA[i] = sm->uartReceiveData[i];
				//set board valid
				sm->ZJB_errCnt = 0;
				StsCtrl.BoardValid |= (1 << 1);
				sm->currState = JTC_COMM_SM_STATE_i_LJB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_100ms)
			{
				//set boart unvalid
				sm->ZJB_errCnt++;
				if(sm->ZJB_errCnt >4)
					StsCtrl.BoardValid &= (~(1 << 1));
				sm->currState = JTC_COMM_SM_STATE_i_LJB;
			}
			break;	
		case JTC_COMM_SM_STATE_i_LJB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_LJB,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_INQUIRY_WORK_INFO,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_LJB;
			break;
		case JTC_COMM_SM_STATE_w_LJB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				for(i=0;i<sm->uartReceiveDataLen;i++)
					sm->LJB_DATA[i] = sm->uartReceiveData[i];
				//set board valid
				sm->LJB_errCnt = 0;
				StsCtrl.BoardValid |= (1 << 2);
				sm->currState = JTC_COMM_SM_STATE_IDEL;
				JTC_COMM_SM_CopyBoardData(sm);
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_100ms)
			{
				//set boart unvalid
				sm->LJB_errCnt++;
				if(sm->LJB_errCnt >4)
					StsCtrl.BoardValid &= (~(1 << 2));
				sm->currState = JTC_COMM_SM_STATE_IDEL;
				JTC_COMM_SM_CopyBoardData(sm);
			}
			break;
			
		//-------------------------------------
		case JTC_COMM_SM_STATE_BROADCAST:
			
			data[0] = (sm->sm_time >> 0) & 0xFF;
			data[1] = (sm->sm_time >> 8) & 0xFF;
			data[2] = (sm->sm_time >>16) & 0xFF;
			data[3] = (sm->sm_time >>24) & 0xFF;
			data[4] = Tax39_Buf[12];
			data[5] = Tax39_Buf[13];
			data[6] = Tax39_Buf[14] & 0x7f;
			data[6] |= Tax39_Buf[15] & 0x80;
		
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_LJB,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_TIME_BROAD_CAST,
								data,
								7);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_s_REC;
			sm->JLB_Xfer_cnt = 0;
			break;
		
		case JTC_COMM_SM_STATE_s_REC:
			sm->JLB_Xfer_cnt++;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JLB_MAIN,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_XFER_STATUS_INFO,
								(uint8_t*)(&StsFrame[StsCtrl.handledIndex]),
								STS_FRAME_SIZE);
		
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_REC;
			break;
		
		case JTC_COMM_SM_STATE_w_REC:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				StsCtrl.handledIndex = STS_FRAME_indexAdd(StsCtrl.handledIndex);
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				if(sm->JLB_Xfer_cnt >= 3)
				{
					StsCtrl.handledIndex = STS_FRAME_indexAdd(StsCtrl.handledIndex);
					sm->currState = JTC_COMM_SM_STATE_IDEL;
				}
				else
				{
					sm->currState = JTC_COMM_SM_STATE_s_REC;
				}
			}
			break;
			
		//--------------external verison--------------------------------------
		case JTC_COMM_SM_STATE_i_ExV_ZKA:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKA,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_ZKA;
			break;
		case JTC_COMM_SM_STATE_w_ExV_ZKA:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_ZKA]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMA1;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMA1;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_JMA1:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMA1,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_JMA1;
			break;
		case JTC_COMM_SM_STATE_w_ExV_JMA1:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_JMA1]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMA2;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMA2;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_JMA2:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMA2,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_JMA2;
			break;
		case JTC_COMM_SM_STATE_w_ExV_JMA2:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_JMA2]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_ZKB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_ZKB;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_ZKB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKB,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_ZKB;
			break;
		case JTC_COMM_SM_STATE_w_ExV_ZKB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_ZJB]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMB1;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMB1;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_JMB1:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMB1,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_JMB1;
			break;
		case JTC_COMM_SM_STATE_w_ExV_JMB1:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_JMB1]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMB2;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_JMB2;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_JMB2:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMB2,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_JMB2;
			break;
		case JTC_COMM_SM_STATE_w_ExV_JMB2:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_JMB2]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_ExV_LJB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_ExV_LJB;
			}
			break;
		case JTC_COMM_SM_STATE_i_ExV_LJB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_LJB,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_Ex_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_ExV_LJB;
			break;
		case JTC_COMM_SM_STATE_w_ExV_LJB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_LJB]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->ex_VersionFlag = 1;
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->ex_VersionFlag = 1;
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			break;
		
		//-----------------internal version-----------------------------------
		case JTC_COMM_SM_STATE_i_InV_ZKA:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKA,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_ZKA;
			break;
		case JTC_COMM_SM_STATE_w_InV_ZKA:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_ZKA]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMA1;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMA1;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_JMA1:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMA1,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_JMA1;
			break;
		case JTC_COMM_SM_STATE_w_InV_JMA1:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_JMA1]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMA2;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMA2;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_JMA2:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMA2,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_JMA2;
			break;
		case JTC_COMM_SM_STATE_w_InV_JMA2:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_JMA2]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_ZKB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_ZKB;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_ZKB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_ZKB,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_ZKB;
			break;
		case JTC_COMM_SM_STATE_w_InV_ZKB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_ZKB]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMB1;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMB1;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_JMB1:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMB1,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_JMB1;
			break;
		case JTC_COMM_SM_STATE_w_InV_JMB1:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_JMB1]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMB2;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_JMB2;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_JMB2:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JMB2,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_JMB2;
			break;
		case JTC_COMM_SM_STATE_w_InV_JMB2:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_JMB2]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				sm->currState = JTC_COMM_SM_STATE_i_InV_LJB;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_i_InV_LJB;
			}
			break;
		case JTC_COMM_SM_STATE_i_InV_LJB:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_LJB,
								JTC_COMM_CMDTYPE_FIRMWARE,
								JTC_COMM_CMD_INQUIRY_In_VERSION,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_InV_LJB;
			break;
		case JTC_COMM_SM_STATE_w_InV_LJB:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_In_LJB]);
				for(i=0;i<sm->uartReceiveDataLen;i++)
					ptr[i] = sm->uartReceiveData[i];
				
				sm->in_VersionFlag = 1;
				sm->currState = JTC_COMM_SM_STATE_s_VERSION;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->in_VersionFlag = 1;
				sm->currState = JTC_COMM_SM_STATE_s_VERSION;
			}
			break;
		case JTC_COMM_SM_STATE_s_VERSION:
			ptr = (uint8_t*)(&sm->SoftVersion[JTC_COMM_VerIndex_Ex_JLSAM]);
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_JLB_MAIN,
								JTC_COMM_CMDTYPE_DEBUG,
								JTC_COMM_CMD_XFER_BOARD_VERSION_INFO,
								ptr,
								JTC_COMM_VERSION_DATALEN);
		
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_VERSION;
			break;
		case JTC_COMM_SM_STATE_w_VERSION:
			if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			break;
		//---------------JW data--------------------------------------
		case JTC_COMM_SM_STATE_i_JW:
			data[0] = 0;
			JTC_COMM_SM_setTxFrame(sm,
								JTC_COMM_BDADDR_LJB,
								JTC_COMM_CMDTYPE_WORK_INFO,
								JTC_COMM_CMD_INQUIRY_110V_INFO,
								data,
								1);
			JTC_COMM_SM_setMonitortick(sm);
			JTC_COMM_SM_clearRxFlag(sm);
			sm->currState = JTC_COMM_SM_STATE_w_JW;
			break;
		case JTC_COMM_SM_STATE_w_JW:
			if(JTC_COMM_SM_receiveNewFrame(sm)){
				
				//copy data
				JTC_COMM_SM_setTxFrame(sm,
									JTC_COMM_BDADDR_JLB_MAIN,
									JTC_COMM_CMDTYPE_WORK_INFO,
									JTC_COMM_CMD_XFER_JW_INFO,
									sm->uartReceiveData,
									sm->uartReceiveDataLen);
				sm->JW_Flag = 1;
				JTC_COMM_SM_setMonitortick(sm);
				JTC_COMM_SM_clearRxFlag(sm);
				//sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			else if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_50ms)
			{
				sm->JW_Flag = 1;
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			}
			break;
			
			
			
		//----------------------------------------------------	
		case JTC_COMM_SM_STATE_ERR:
			if(JTC_COMM_SM_tickElapsed(sm) > JTC_COMM_SM_TICK_100ms)
				sm->currState = JTC_COMM_SM_STATE_IDEL;
			break;
		default:
			break;
	}
	return;
}
//------------------------------receive-----------------------------------------
///
void JTC_COMM_SM_clearRxFlag(JTC_COMM_SM *sm)
{
	sm->uartReceiveFlag = 0;
	return;
}
///
void JTC_COMM_SM_setRxFlag(JTC_COMM_SM *sm)
{
	sm->uartReceiveFlag = 1;
	return;
}
///
uint8_t JTC_COMM_SM_receiveNewFrame(JTC_COMM_SM *sm)
{
	if(sm->uartReceiveFlag)
		return 1;
	return 0;
}
///
void JTC_COMM_SM_setRxFrame(JTC_COMM_SM *sm,
								uint8_t src,
								uint8_t type,
								uint8_t cmd,
								uint8_t *dataBuf,
								uint16_t len)
{
	uint16_t i;
	
//	if(!JTC_COMM_SM_haveFrameToTx(sm))
//		return 1;
	
	sm->uartReceiveSrc = src;
	sm->uartReceiveType = type;
	sm->uartReceiveCmd = cmd;
	sm->uartReceiveDataLen = len;
	for(i=0; i< len; i++){
		sm->uartReceiveData[i] = dataBuf[i];
	}
	
	JTC_COMM_SM_setRxFlag(sm);
	return;
}
//------------------------------transmit-----------------------------------------
///
uint8_t JTC_COMM_SM_haveFrameToTx(JTC_COMM_SM *sm)
{
	if(sm->uartTransmitFlag != 0)
		return 1;
	return 0;
}
///
void JTC_COMM_SM_clearTxFlag(JTC_COMM_SM *sm)
{
	sm->uartTransmitFlag = 0;
	return;
}
///
void JTC_COMM_SM_setTxFlag(JTC_COMM_SM *sm)
{
	sm->uartTransmitFlag = 1;
	return;
}
///
void JTC_COMM_SM_setTxFrame(JTC_COMM_SM *sm,
							uint8_t dst,
							uint8_t type,
							uint8_t cmd,
							uint8_t *dataBuf,
							uint16_t len)
{
	uint16_t i;
	
//	if(!JTC_COMM_SM_haveFrameToTx(sm))
//		return 1;
	
	sm->uartTransmitDst = dst;
	sm->uartTransmitType = type;
	sm->uartTransmitCmd = cmd;
	sm->uartTransmitDataLen = len;
	for(i=0; i< len; i++){
		sm->uartTransmitData[i] = dataBuf[i];
	}
	
	JTC_COMM_SM_setTxFlag(sm);
	return;
}
///
uint8_t JTC_COMM_SM_takeTxFrame(JTC_COMM_SM *sm,
									uint8_t *dst,
									uint8_t *type,
									uint8_t *cmd,
									uint8_t *readDataBuf,
									uint16_t *len)
{
	uint16_t i;
	
	if(!dst || !type || !cmd || !readDataBuf || !len)
		return 1;
	
	if(!JTC_COMM_SM_haveFrameToTx(sm))
		return 1;
	
	*dst = sm->uartTransmitDst;
	*type = sm->uartTransmitType;
	*cmd = sm->uartTransmitCmd;
	*len = sm->uartTransmitDataLen;
	for(i=0; i< sm->uartTransmitDataLen; i++){
		readDataBuf[i] = sm->uartTransmitData[i];
	}
	
	JTC_COMM_SM_clearTxFlag(sm);
	
	return 0;
}
//-----------------------------------------------------------------------






///EOF
